"""
@ -*- coding: utf-8 -*-
@ IDE: PyCharm
@ Author: 陈雄伟
@ DateTime: 2022/6/16 19:06 
@ FileName: my_project 
@ ProjectName:shopmiddleware
"""


#自定义中间类（执行是否登录判断）
from django.shortcuts import redirect
from django.urls import reverse
import re

class ShopMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response
        print("ShopMiddleware")

    def __call__(self, request):
        path = request.path
        print("url:", path)

        # 判断管理后台是否登录
        # 判断当前请求URL地址是否是以/myadmin开头,并且不在urllist中，才做是否登录判断，防止出现死循环
        urllist = ['/myadmin/login','/myadmin/logout','/myadmin/dologin',"/myadmin/verify"]
        if re.match(r'^/myadmin',path) and (path not in urllist):
            # 判断是否登录，判断session有没有adminuser
            if 'adminuser' not in request.session:
                return redirect(reverse('myadmin_login'))

        #判断大堂点餐请求的判断是否登录（session是否有webuser）
        if re.match(r'^/web',path):
            # 判断是否登录，判断session有没有adminuser
            if 'webuser' not in request.session:
                return redirect(reverse('web_login'))

            # 判断移动端是否登录
            # 定义移动端不登录可以直接访问的路由
        urllist = ['/mobile/register',"/mobile/doregister"]
        if re.match(r'^/mobile', path) and (path not in urllist):
            # 判断是否登录，判断session有没有mobileuser
            if 'mobileuser' not in request.session:
                return redirect(reverse('mobile_register'))

        response = self.get_response(request)
        # Code to be executed for each request/response after
        # the view is called.
        return response